import Vue from 'vue'
import SSAP from "save-svg-as-png";

const axios = Vue.$axios

const BlobDownload = (data, option, fileName) => {
    let href = window.URL.createObjectURL(new Blob([data], option))
    let a = document.createElement('a')
    a.style.display = 'none'
    a.href = href
    a.download = fileName
    let event = new MouseEvent('click')
    a.dispatchEvent(event)
    a.remove()
}

const SsapDownload = (data, fileName) => {
    let a = document.createElement('a')
    a.style.display = 'none'
    a.innerHTML = data
    SSAP.saveSvgAsPng(a.firstChild, fileName)
    a.remove()
}

export const DownloadAvatarAsSvg = (href, fileName) => {
    let data
    fileName = fileName || "avatar"
    axios({
        url: href,
        method: 'get',
    }).then((res) => {
        data = res.data
        BlobDownload(data, {type: "image/svg+xml"}, fileName + ".svg")
    })
}

export const DownloadAvatarAsPng = (href, fileName) => {
    let data
    fileName = fileName || "avatar"
    axios({
        url: href,
        method: 'get',
    }).then((res) => {
        data = res.data
        SsapDownload(data, fileName + ".png")
    })
}